package com.luban;


import com.luban.entity.Person;
import com.luban.entity.User;
import com.luban.service.*;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.aop.framework.autoproxy.TargetSourceCreator;
import org.springframework.beans.factory.annotation.Autowire;
import org.springframework.beans.factory.config.CustomEditorConfigurer;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.*;
import org.springframework.context.support.ConversionServiceFactoryBean;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.convert.ConversionService;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.beans.PropertyEditor;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


@ComponentScan("com.luban")
//这样注册到容器中的bean，beanName是类的全限定名
//@Import(value = {User.class})

//@Import(value = {LubanImportBeanDefinitionRegistrar.class})

@Configuration

//@ImportResource(value = "classpath:spring.xml")

//@Import(value = LubanBeanDefinitionRegistrar.class)


@MapperScan(basePackages = "com.luban.dao")
//@EnableAspectJAutoProxy
@EnableTransactionManagement
public class AppConfig {


	///**
	// * 自动代理 BeanPostProcessor
	// *
	// * @return
	// */
	//@Bean
	//public BeanNameAutoProxyCreator creator() {
	//	BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
	//	beanNameAutoProxyCreator.setBeanNames("userService");
	//	beanNameAutoProxyCreator.setInterceptorNames("myAdvisor");
	//	return beanNameAutoProxyCreator;
	//}

	///**
	// * 哪些Bean需要被代理
	// * 代理逻辑
	// *
	// * 添加了BeanPostProcessor
	// *
	// * @return
	// */
	//@Bean
	//public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
	//
	//	DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
	//
	//	creator.setCustomTargetSourceCreators(new TargetSourceCreator() {
	//		@Override
	//		public TargetSource getTargetSource(Class<?> beanClass, String beanName) {
	//			return null;
	//		}
	//	});
	//
	//	return creator;
	//}


	/**
	 * BeanDefinition，beanClass=null，factoryBean=AppConfig，factoryMethod=user
	 *
	 * @return
	 */
	@Bean
	public User user() {
		User user = new User();
		return user;

	}


	//@Bean
	//public static MapperScannerConfigurer configurer() {
	//	MapperScannerConfigurer configurer = new MapperScannerConfigurer();
	//	configurer.setBasePackage("com.luban");
	//	return configurer;
	//}



	//@Bean(autowire = Autowire.BY_TYPE)
	//public OrderService orderService() {
	//	return new OrderService();
	//}



	/**
	 * 配置国际化资源
	 *
	 * @return
	 */
	//@Bean
	//public MessageSource messageSource() {
	//	ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
	//	messageSource.setBasename("messages");
	//	return messageSource;
	//}

	/**
	 * 定义一个监听器
	 *
	 * @return
	 */
	//@Bean
	//public ApplicationListener<ApplicationEvent> applicationListener() {
	//	return event -> System.out.println("收到了一个事件");
	//}

	/**
	 * Java自定义类型转换器的配置类
	 *
	 * @return
	 */
	//@Bean
	//public CustomEditorConfigurer customEditorConfigurer() {
	//	CustomEditorConfigurer customEditorConfigurer = new CustomEditorConfigurer();
	//	Map<Class<?>, Class<? extends PropertyEditor>> propertyEditorMap = new HashMap<>();
	//	propertyEditorMap.put(User.class, StringToUserPropertyEditor.class);
	//	customEditorConfigurer.setCustomEditors(propertyEditorMap);
	//	return customEditorConfigurer;
	//}

	/**
	 * Spring的类型转换器
	 *
	 * @return
	 */
	//@Bean
	//public ConversionServiceFactoryBean conversionService() {
	//	ConversionServiceFactoryBean conversionServiceFactoryBean = new ConversionServiceFactoryBean();
	//	conversionServiceFactoryBean.setConverters(Collections.singleton(new StringToUserConverter()));
	//	return conversionServiceFactoryBean;
	//}



	// 自动代理 BeanPost
//	@Bean
//	public BeanNameAutoProxyCreator creator(){
//		// BeanPostProcessor
//		BeanNameAutoProxyCreator beanNameAutoProxyCreator = new BeanNameAutoProxyCreator();
//		beanNameAutoProxyCreator.setBeanNames("userService");  // bean==>AOP
//		beanNameAutoProxyCreator.setInterceptorNames("myAdvisor");
//		return beanNameAutoProxyCreator;
//	}

	// 哪些bean需要被代理？  MyAdvisor  PointCut
	// 代理逻辑
	// 添加了BeanPostProcessor
//	@Bean
//	public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
//		DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
//		return creator;
//	}

//	@Bean
//	public  MapperScannerConfigurer configurer(){
//		MapperScannerConfigurer configurer = new MapperScannerConfigurer();
//		configurer.setBasePackage("com.luban.dao");
//
//		return configurer;
//	}
//
	@Bean
	public DataSource dataSource() {
		DriverManagerDataSource dataSource = new DriverManagerDataSource();
		dataSource.setUrl("jdbc:mysql://localhost:3306/luban?useSSL=false");
		dataSource.setUsername("root");
		dataSource.setPassword("root");
		return dataSource;
	}

	@Bean
	public SqlSessionFactoryBean sqlSessionFactory() {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource());
		return sqlSessionFactoryBean;
	}

	// 事务管理器
	@Bean
	public PlatformTransactionManager transactionManager() {
		DataSourceTransactionManager transactionManager = new DataSourceTransactionManager();
		transactionManager.setDataSource(dataSource());

		// 设置要不要把事务信息同步到线程中
//		transactionManager.setTransactionSynchronization();

		return transactionManager;
	}


}
